package cn.itcast.bos.web.action.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.google.common.util.concurrent.Service;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;

@Actions
@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("propotype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard> {
	private Standard standard = new Standard();

	@Override
	public Standard getModel() {
		return standard;
	}

	@Resource
	private StandardService Service;

	// 添加收派标准
	@Action(value = "standard_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/standard.html") })
	public String save() {
		Service.save(standard);
		return SUCCESS;
	}

	// 查询收派标准(分页查询)
	// 使用属性封装数据
	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	@Action(value = "standard_queryPage", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 调用业务层,查询数据结果
		Pageable pageable = new PageRequest(page - 1, rows);// 这个方法可以很简便的封住一个储存分页数据的pageBean,底层是通过当前页和每页显示条数来查询封装整个分页需要的所有数据
		Page<Standard> pageData = Service.findPageData(pageable);
		Map<String, Object> map = new HashMap<>();
		map.put("total", pageData.getTotalElements());
		map.put("rows", pageData.getContent());
		ServletActionContext.getContext().getValueStack().push(map);
		;
		return SUCCESS;
	}

	// 查询所有分派标准的方法
	@Action(value = "standard_findAll", results = { @Result(name = "success", type = "json") })
	public String findAll() {
		// 从数据库获取派信息
		List<Standard> li = Service.findAll();
		// 将数据压入栈顶
		ServletActionContext.getContext().getValueStack().push(li);
		return SUCCESS;
	}
}
